﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using MyGeneration.dOOdads;

namespace WebUI.EditPages
{
    public partial class ChangeLoanPhone : BasePage
    {
        int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (Request.QueryString[ID] != null && Request.QueryString[ID] != "")
                id = int.Parse(Request.QueryString[ID]);
            else
            {
                CloseDlg(0);
                return;
            }

            if (!IsPostBack)
            {
                RepairJob job = new RepairJob();

                job.LoadByPrimaryKey(id);
                if (job.RowCount <= 0)
                {
                    CloseDlg(0);
                    return;
                }

                if (job.s_LoanPhoneID != "")
                {
                    POSSystem.BLL.LoanPhones lp = new POSSystem.BLL.LoanPhones();
                    lp.LoadByPrimaryKey(job.LoanPhoneID);
                    txtLoanIMEI.Text = lp.s_IMEI;
                    txtLoanPhone.Text = lp.s_ProductName;
                }
            }
        }

        public void btnSave_OnClick(Object sender, EventArgs e)
        {
            if(Save())
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("window.returnValue={0};", 1), true);
        }

        private bool Save()
        {
            TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();

            try
            {
                tx.BeginTransaction();

                RepairJob job = new RepairJob();

                job.LoadByPrimaryKey(id);
                if (job.RowCount > 0)
                {
                    POSSystem.BLL.LoanPhones lp = null;

                    if (job.s_LoanPhoneID != "" && txtLoanIMEI.Text.Trim() != job.s_LoanPhoneID)
                    {
                        lp = new POSSystem.BLL.LoanPhones();
                        lp.LoadByPrimaryKey(job.LoanPhoneID);
                        if (lp.RowCount > 0)
                        {
                            lp.Free = true;
                            lp.Save();
                        }
                    }

                    lp = POSSystem.BLL.LoanPhones.GetLoanPhoneByIMEI(txtLoanIMEI.Text.Trim());
                    if (lp.RowCount > 0)
                    {
                        lp.Free = false;
                        lp.Save();
                        job.LoanPhoneID = lp.ID;
                        job.LoanPhoneReturn = false;
                    }
                    else
                        job.s_LoanPhoneID = "";

                    job.Save();

                    if (!string.IsNullOrEmpty(Request.QueryString[KEY]))
                    {
                        View_RepairJobs jobs = (View_RepairJobs)Session[Request.QueryString[KEY]];
                        if (jobs != null)
                        {
                            string oldFilter = jobs.Filter;
                            jobs.Filter = View_RepairJobs.ColumnNames.ID + "=" + job.s_ID;
                            if (jobs.RowCount > 0)
                            {
                                jobs.s_LoanPhoneID = job.s_LoanPhoneID;
                                jobs.s_LoanPhoneReturn = job.s_LoanPhoneReturn;
                                if (lp != null && lp.RowCount > 0)
                                {
                                    jobs.s_LoanPhoneName = lp.s_ProductName;
                                    jobs.s_LoanPhoneIMEI = lp.s_IMEI;
                                }
                            }

                            jobs.Filter = oldFilter;
                        }
                    }
                }

                tx.CommitTransaction();
                ((Popup)Master).ShowMessage("Saved");
                return true;
            }
            catch (Exception ex)
            {
                tx.RollbackTransaction();
                ErrorLog.WriteToFile("", "", ex.StackTrace);
                ((Popup)Master).ShowErrorMessage("Error: " + ex.Message);
                return false;
            }

        }

        public void btnSaveAndClose_OnClick(Object sender, EventArgs e)
        {
            if(Save())
                CloseDlg(1);
        }

        protected void txtLoanIMEI_OnTextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtLoanIMEI.Text.Trim()))
            {
                POSSystem.BLL.LoanPhones p = POSSystem.BLL.LoanPhones.GetLoanPhoneByIMEI(txtLoanIMEI.Text);
                if (p.RowCount > 0)
                {
                    if (!p.Free || p.Deleted)
                    {
                        txtLoanIMEI.Text = "";
                        txtLoanPhone.Text = "This phone is not available.";
                        txtLoanPhone.CssClass = "labelError";
                        return;
                    }

                    txtLoanPhone.Text = p.ProductName;
                    txtLoanPhone.CssClass = "labelMessage";
                }
                else
                {
                    txtLoanIMEI.Text = "";
                    txtLoanPhone.Text = "No existing loan phone has this IMEI number.";
                    txtLoanPhone.CssClass = "labelError";
                }
            }
            else
            {
                txtLoanPhone.Text = "";
            }
        }
    }
}